package cn.nebula.util.lucene.directory;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;

import cn.nebula.util.lucene.utils.File2DocumentUtils;


public class DirectoryTest {
	String filePath = "luceneDatasource\\IndexWriter addDocument 's javadoc.txt";
	String indexPath = "luceneIndex";
	Analyzer analyzer = new StandardAnalyzer();

	public void test1()throws Exception {
		// Directory dir = FSDirectory.getDirectory(indexPath);
		Directory dir = new RAMDirectory();
		
		Document doc = File2DocumentUtils.file2Document(filePath);
		IndexWriter indexWriter = new IndexWriter(dir, analyzer, MaxFieldLength.LIMITED);
		indexWriter.addDocument(doc);
		indexWriter.close();
	}
	
	public void test2() throws Exception{
		Directory fsDir = FSDirectory.getDirectory(indexPath);
	
		Directory ramDir = new RAMDirectory(fsDir);
		
		IndexWriter ramIndexWriter = new IndexWriter(ramDir, analyzer, MaxFieldLength.LIMITED);
		Document doc = File2DocumentUtils.file2Document(filePath);
		ramIndexWriter.addDocument(doc);
		ramIndexWriter.close();
		
		IndexWriter fsIndexWriter = new IndexWriter(fsDir, analyzer,true, MaxFieldLength.LIMITED);
		fsIndexWriter.addIndexesNoOptimize(new Directory[]{ramDir});
		//	fsIndexWriter.flush();
		//	fsIndexWriter.optimize();
		fsIndexWriter.close();
	}
	
	
	public void test3() throws Exception{
		Directory fsDir = FSDirectory.getDirectory(indexPath);
		IndexWriter fsIndexWriter = new IndexWriter(fsDir, analyzer, MaxFieldLength.LIMITED);
		
		fsIndexWriter.optimize();
		fsIndexWriter.close();
	}
}
